Implement select-by-words for selectable labels
authorMatthias Clasen <matthiasc@src.gnome.org>
Thu, 22 Jan 2009 00:33:54 +0000 (00:33 +0000)
committerMatthias Clasen <matthiasc@src.gnome.org>
Thu, 22 Jan 2009 00:33:54 +0000 (00:33 +0000)
svn path=/trunk/; revision=22168

ChangeLog
gtk/gtklabel.c

index f780863c9329255fcae20c21104dedf98839cbe5..3c43b84962e3c1d4e7e3bffcb094a11338b122cd 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+2009-01-21  Matthias Clasen <mclasen@redhat.com>
+
+       * gtk/gtklabel.c: Implement select-by-words for selectable labels.
+
 2009-01-21  Tor Lillqvist  <tml@novell.com>
 
        * gdk/win32/gdkcursor-win32.c: Rename static local functions and
index 229d7b07bcac62c0c3387ad0cf462be7aff6afe2..209c3692e8856553e4a6113574db11be39aeea24 100644 (file)
@@ -69,6 +69,7 @@ struct _GtkLabelSelectionInfo
   gint drag_start_y;
 
   guint in_drag : 1;
+  guint select_words : 1;
 };
 
 enum {
@@ -3335,6 +3336,8 @@ gtk_label_button_press (GtkWidget      *widget,
     return FALSE;
 
   label->select_info->in_drag = FALSE;
+  label->select_info->select_words = FALSE;
+
   if (event->button == 1)
     {
       if (!GTK_WIDGET_HAS_FOCUS (widget)) 
@@ -3352,6 +3355,7 @@ gtk_label_button_press (GtkWidget      *widget,
       
       if (event->type == GDK_2BUTTON_PRESS)
        {
+          label->select_info->select_words = TRUE;
          gtk_label_select_word (label);
          return TRUE;
        }
@@ -3504,7 +3508,6 @@ gtk_label_motion (GtkWidget      *widget,
   if (label->select_info == NULL)
     return FALSE;  
 
-
   if ((event->state & GDK_BUTTON1_MASK) == 0)
     return FALSE;
 
@@ -3537,9 +3540,48 @@ gtk_label_motion (GtkWidget      *widget,
     {
       get_layout_index (label, x, y, &index);
       
-      gtk_label_select_region_index (label,
-                                    label->select_info->selection_anchor,
-                                    index);
+      if (label->select_info->select_words)
+        {
+          gint min, max;
+          gint old_min, old_max;
+          gint anchor, end;
+
+          min = gtk_label_move_backward_word (label, index);
+          max = gtk_label_move_forward_word (label, index);
+
+          anchor = label->select_info->selection_anchor;
+          end = label->select_info->selection_end;
+
+          old_min = MIN (anchor, end);
+          old_max = MAX (anchor, end);
+
+          if (min < old_min)
+            {
+              anchor = min;
+              end = old_max;
+            }
+          else if (old_max < max)
+            {
+              anchor = max;
+              end = old_min;
+            }
+          else if (anchor == old_min)
+            {
+              if (anchor != min)
+                anchor = max;
+            }
+          else
+            {
+              if (anchor != max)
+                anchor = min;
+            }
+
+          gtk_label_select_region_index (label, anchor, end);
+        }
+      else
+        gtk_label_select_region_index (label, 
+                                       label->select_info->selection_anchor,
+                                       index);
     }
 
   return TRUE;